package conf

import (
	"common/redis"
	gcfg "gopkg.in/gcfg.v1"
	"runtime"
)

var (
	Conf *Config
)

type Config struct {
	Base     struct {
			 MaxProc   int      `gcfg:"maxproc"`
			 PidFile   string   `gcfg:"pid"`
			 PprofAddr []string `gcfg:"pprof"`
			 LogPath   string   `gcfg:"log"`
			 Debug     bool     `gcfg:"debug"`
		 }
	Tcp      struct {
			 Addr    string
			 Timeout int64
		 }

	Mysql    struct {
			 Addr    string
			 MaxOpen int
			 MaxIdle int
		 }
	Kafka    struct {
			 ZKList     []string
			 BrokerList []string
			 GroupId    string
		 }
	Producer struct {
			 Addrs []string
		 }
	Redis    redis.Config
	Table    struct {
			 ProductId []string
		 }

	PidKafkas   map[string]*PidKafka
}

type PidKafka struct {
	Address string
	Topic   string
}

func init() {
	Conf = &Config{}
	Conf.Base.MaxProc = runtime.NumCPU()
}

// Init init the configuration file.
func Init(confPath string) error {

	if err := gcfg.ReadFileInto(Conf, confPath); err != nil {
		return err
	}
	return nil
}

// Reload reload the configuration file.
func Reload(confPath string) error {
	tmp := &Config{}
	if err := gcfg.ReadFileInto(tmp, confPath); err != nil {
		return err
	}
	Conf = tmp
	return nil
}
